package huoye.shops.huoye.operation.networt;


import android.util.Log;

import com.codeideology.android.http.okhttp.OkHttpUtils;
import com.codeideology.android.lang.entity.Paging;
import com.codeideology.android.rpc.BaseJsonRequestCallback;
import com.codeideology.android.utils.StringUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import huoye.shops.base.APIUrls;
import huoye.shops.base.BaseService;
import huoye.shops.base.MineBaseJsonRequestCallback;
import huoye.shops.base.UserManager;
import huoye.shops.huoye.mine.model.PostDepositRec;
import huoye.shops.huoye.mine.model.PowerList;
import huoye.shops.huoye.mine.model.ProductInfo;
import huoye.shops.huoye.operation.model.Descript;
import huoye.shops.huoye.operation.model.DiscountCancelData;
import huoye.shops.huoye.operation.model.EventInfo;
import huoye.shops.huoye.operation.model.EventOddPrizeList;
import huoye.shops.huoye.operation.model.GuestsDetailData;
import huoye.shops.huoye.operation.model.ManageEventData;
import huoye.shops.huoye.operation.model.ObtainShopType;
import huoye.shops.huoye.operation.model.OrderInfo;
import huoye.shops.huoye.operation.model.PlatformInfo;
import huoye.shops.huoye.operation.model.SellOrder;
import huoye.shops.huoye.operation.model.SharePondData;
import huoye.shops.huoye.operation.model.ShopCouponsList;
import huoye.shops.huoye.operation.model.ShopEventPrizeList;
import huoye.shops.huoye.operation.model.ShopPrizeList;
import huoye.shops.huoye.operation.model.ShopProductList;
import huoye.shops.huoye.operation.model.StatisticsData;
import huoye.shops.huoye.operation.model.ToUrl;
import huoye.shops.huoye.operation.model.UploadingEventOddPrizeList;
import huoye.shops.huoye.operation.model.VipGustsData;
import huoye.shops.huoye.user.model.User;
import huoye.shops.utils.StockUtils;


/**
 * Created by user on 2017/7/24.
 */

public class OperationService extends BaseService {


    /**
     * 获取订单详情
     *
     * @param orderId
     * @param callback
     */
    public void getOrderInfo(String orderId, MineBaseJsonRequestCallback<OrderInfo> callback) {

        Map<String, String> param = new HashMap<>();
        param.put("orderId", orderId);


        OkHttpUtils.getInstance().get()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_GET_ORDER_INFO))
                .params(param)
                .build().execute(callback);


    }

    /**
     * 商家修改订单状态
     *
     * @param orderId
     * @param callback
     */
    public void setOrderState(String orderId, String action, String sendType, MineBaseJsonRequestCallback<OrderInfo> callback) {
        Map<String, String> param = new HashMap<>();

        param.put("orderId", orderId);
        param.put("action", action);
        if (StringUtils.isNotEmpty(sendType)) {
            param.put("sendType", sendType);
        }

        setUser(param);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_SET_ORDER_STATE))
                .params(param)
                .build().execute(callback);


    }

    /**
     * 获取平台配送信息
     *
     * @param callback
     */
    public void getPlatformInfo(MineBaseJsonRequestCallback<PlatformInfo> callback) {
        Map<String, String> param = new HashMap<>();
        String angentId = UserManager.getInstance().getAngentId();
        param.put("angentId",angentId);

        setUser(param);

        OkHttpUtils.getInstance().get()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_GET_PLATFORM_INFO))
                .params(param)
                .build().execute(callback);


    }


    /**
     * 获取订单列表
     *
     * @param state
     * @param paging
     * @param callback
     */
    public void getOrderList(int state, Paging paging, MineBaseJsonRequestCallback<SellOrder> callback) {

        Map<String, String> param = new HashMap<>();

        setUser(param);
        param.put("state", state + "");
        setPaging(param, paging);


        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_ORDER_LIST))
                .params(param)
                .build().execute(callback);


    }


    /**
     * 获取商家奖品列表
     *
     * @param keyword
     * @param type
     * @param paging
     * @param callback
     */
    public void getPrizeList(String keyword, String type,Paging paging, MineBaseJsonRequestCallback<ShopPrizeList> callback) {
        Map<String, String> param = new HashMap<>();
        if (StringUtils.isNotEmpty(keyword)) {
            param.put("keyword", keyword);
        }

        if (StringUtils.isNotEmpty(type)) {
            param.put("type",type);
        }

        setUserShopList(param, paging);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_PRIZE_LIST))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 商家获取商品列表
     *
     * @param keyword
     * @param productType
     * @param paging
     * @param callback
     */
    public void getProductList(String keyword,int productType, Paging paging, MineBaseJsonRequestCallback<ShopProductList> callback) {
        Map<String, String> param = new HashMap<>();
        if (StringUtils.isNotEmpty(keyword)) {
            param.put("keyword", keyword);
        }
        param.put("productType",productType+"");

        setUserShopList(param, paging);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_PRODUCT_LIST))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 商家商品删除
     *
     * @param productId
     * @param callback
     */
    public void getProductInfo(String productId, MineBaseJsonRequestCallback<ProductInfo> callback) {
        Map<String, String> param = new HashMap<>();
        param.put("productId", productId);
        setUser(param);

        OkHttpUtils.getInstance().get()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_GET_PRODUCT_INFO))
                .params(param)
                .build().execute(callback);
    }





    /**
     * 获取优惠券列表
     *
     * @param callback
     */
    public void getShopGoupons(MineBaseJsonRequestCallback<ShopCouponsList> callback) {
        Map<String, String> param = new HashMap<>();

        OkHttpUtils.getInstance().get()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_GET_SHOP_GOUPONS))
                .params(param)
                .build().execute(callback);
    }



    /**
     * 商家商品删除
     *
     * @param productId
     * @param callback
     */
    public void delProduct(String productId, MineBaseJsonRequestCallback<User> callback) {
        Map<String, String> param = new HashMap<>();
        param.put("productId", productId);
        setUser(param);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_PRODUCT_DEL))
                .params(param)
                .build().execute(callback);
    }

    /**
     * 商家商品上传/编辑
     *
     * @param productId   //商品ID
     * @param productTile 商品标题
     * @param salePrise   销售价格
     * @param saleNum
     * @param isSale
     * @param marjorImg   主图集合（图片地址,图片地址）
     * @param disType     折扣类型（0：不参与折扣 1：新用户折扣 2：一卡通折扣 3.联盟折扣 4：特权折扣（5折以下） 5:自定义折扣（五折以上））
     * @param disNum
     * @param couponId
     * @param limitStart
     * @param limitEnd
     * @param callback
     */
    public void infoProduct(String productId, String productTile, String salePrise, String saleNum, String isSale, List<String> marjorImg,
                            String disType, String disNum, List<Descript> descripts, String couponId,
                            String limitStart, String limitEnd,
                            MineBaseJsonRequestCallback<User> callback) {
        Map<String, String> param = new HashMap<>();
        param.put("productId", productId);
        param.put("productTile", productTile);
        param.put("salePrise", salePrise);
        param.put("saleNum", saleNum);
        param.put("isSale", isSale);
        param.put("disType", disType);


        Gson gson = new Gson();

        String descriptslist = gson.toJson(descripts);
        param.put("descript", descriptslist);

        if(marjorImg.size() >0){
            param.put("marjorImg", StockUtils.listToString(marjorImg));
        }

        if (StringUtils.isNotEmpty(disNum)) {
            param.put("disNum", disNum);
        }
        if (StringUtils.isNotEmpty(couponId)) {
            param.put("couponId", couponId);
        }
        if (StringUtils.isNotEmpty(limitStart)) {
            param.put("limitStart", limitStart);
        }
        if (StringUtils.isNotEmpty(limitEnd)) {
            param.put("limitEnd", limitEnd);
        }
        setUser(param);
        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_PRODUCT_INFO))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 核销折扣列表
     *
     * @param state
     * @param paging
     * @param callback
     */
    public void getShopDiscountList(String state, Paging paging, MineBaseJsonRequestCallback<DiscountCancelData> callback) {
        Map<String, String> param = new HashMap<>();
        param.put("state", state);
        setUserShopId(param);
        setPaging(param, paging);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_SHOP_DISCOUNT_LIST))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 奖品上传
     *
     * @param
     * @param
     * @param callback
     */
    public void savePrize(String prizeTitle, String prizeSale, String prizeNum, String prizeImg, int type, MineBaseJsonRequestCallback<User> callback) {
        Map<String, String> param = new HashMap<>();

        setUser(param);
        param.put("prizeTitle", prizeTitle);
        param.put("prizeSale", prizeSale);
        param.put("prizeNum", prizeNum);
        param.put("prizeImg", prizeImg);
        if (type == 1) {
            param.put("type", "add");
        } else {
            param.put("type", "edit");
        }


        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_SAVE_PRIZE))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 获取用户统计信息
     *
     * @param type
     * @param paging
     * @param callback
     */
    public void getUserStatisticsList(String type, Paging paging, MineBaseJsonRequestCallback<StatisticsData> callback) {
        Map<String, String> param = new HashMap<>();
        param.put("dataType", type);
        setUserShopList(param,paging);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_USER_STATISTICS))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 获取活动列表
     *
     * @param type
     * @param paging
     * @param callback
     */
    public void getEventList(int type, Paging paging, MineBaseJsonRequestCallback<ManageEventData> callback) {
        Map<String, String> param = new HashMap<>();
        param.put("state", type + "");
        setUserShopList(param, paging);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_EVENT_LIST))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 商家创建活动（目前只创建共享奖池）
     *
     * @param remarks
     * @param beginTime
     * @param endTime
     * @param userLotteryNum
     * @param mostWinsNum
     * @param lists
     * @param callback
     */
    public void EventCreate(String remarks, String beginTime, String endTime, String userLotteryNum,
                            String mostWinsNum, List<ShopEventPrizeList> lists,MineBaseJsonRequestCallback<User> callback) {
        Map<String, String> param = new HashMap<>();

        param.put("remarks", remarks);
        param.put("beginTime", beginTime);
        param.put("endTime", endTime);
        param.put("userLotteryNum", userLotteryNum);
        param.put("mostWinsNum", mostWinsNum);

        Gson gson = new Gson();
        String s = gson.toJson(lists);
        param.put("shopEventPrizeList", s);
        setUser(param);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_EVENT_CREATE))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 获取活动详情
     *
     * @param eventId
     * @param callback
     */
    public void getEventDetail(String eventId, MineBaseJsonRequestCallback<SharePondData> callback) {

        Map<String, String> param = new HashMap<>();
        param.put("eventId", eventId);
        setUser(param);


        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_NEW_EVENT_DETAIL))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 商家参加活动
     *
     * @param sharePondData
     * @param callback
     */
    public void joinEvent(SharePondData sharePondData, MineBaseJsonRequestCallback<SharePondData> callback) {

        EventInfo eventInfo = sharePondData.getEventInfo();


        Map<String, String> param = new HashMap<>();
        param.put("eventId", eventInfo.getEventId());

        List<EventOddPrizeList> eventOddPrizeList = sharePondData.getSharePool().getEventOddPrizeList();
        List<UploadingEventOddPrizeList> list = new ArrayList<>();
        for (int i = 0; i < eventOddPrizeList.size(); i++) {
            UploadingEventOddPrizeList uploading = new UploadingEventOddPrizeList();
            if (eventInfo.getEtClassType() == 1) {
                uploading.setEventOddId(eventOddPrizeList.get(i).getEventOddId());
            }
            if(StringUtils.isNotEmpty(eventOddPrizeList.get(i).getPrizeNum())){
                uploading.setNum(eventOddPrizeList.get(i).getPrizeNum());
            }
            uploading.setPrizeId(eventOddPrizeList.get(i).getPrizeId());
            list.add(uploading);
        }



        Gson gson = new Gson();
        String s = gson.toJson(list);
        param.put("eventPrizeList", s);

       /* for (int i = 0 ; i < eventOddPrizeList.size();i++){
            param.put("prizeId",eventOddPrizeList.get(i).getPrizeId());
            param.put("num", eventOddPrizeList.get(i).getPrizeNum());
            if(eventInfo.getEtClassCode().equals("gxjc")){
                param.put("eventOddId", eventOddPrizeList.get(i).getEventOddId());
            }
        }*/


        setUser(param);


        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_EVENT_JOIN))
                .params(param)
                .build().execute(callback);

    }

    /**
     * 商家取消活动
     *
     * @param eventId
     * @param callback
     */
    public void cancelEvent(String eventId, MineBaseJsonRequestCallback<SharePondData> callback) {


        Map<String, String> param = new HashMap<>();
        param.put("eventId", eventId);
        setUser(param);


        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_EVENT_CANCEL))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 生成活动扫描二维码URL
     *
     * @param eventId
     * @param callback
     */
    public void eventToUrl(String eventId, MineBaseJsonRequestCallback<ToUrl> callback) {

        Map<String, String> param = new HashMap<>();
        param.put("eventId", eventId);
        setUserShopId(param);


        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_EVENT_TO_URL))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 获取特权用户列表
     *
     * @param paging
     * @param callback
     */
    public void postPrivilegedGuestList(Paging paging, MineBaseJsonRequestCallback<VipGustsData> callback) {
        Map<String, String> param = new HashMap<>();

        setUserShopList(param, paging);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_PRIVILEGED_GUEST_LIST))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 删除用户特权接口
     *
     * @param shopSpeGuestId
     * @param callback
     */
    public void delPrivilegedGuest(String shopSpeGuestId, MineBaseJsonRequestCallback<VipGustsData> callback) {
        Map<String, String> param = new HashMap<>();

        param.put("shopSpeGuestId", shopSpeGuestId);
        setUser(param);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_DEL_PRIVILEGED_GUEST))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 获取特权用户详情
     *
     * @param shopSpeGuestId
     * @param callback
     */
    public void getPrivilegedGuestDetail(String shopSpeGuestId, MineBaseJsonRequestCallback<GuestsDetailData> callback) {
        Map<String, String> param = new HashMap<>();

        param.put("shopSpeGuestId", shopSpeGuestId);
        setUser(param);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_PRIVILEGED_GUEST_DETAIL))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 获取当前用户的权限列表
     *
     * @param callback
     */
    public void getUserPowers( MineBaseJsonRequestCallback<PowerList> callback) {
        Map<String, String> param = new HashMap<>();

       // param.put("shopSpeGuestId", shopSpeGuestId);
        setUser(param);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_USER_POWERS))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 修改用户特权次数
     *
     * @param shopSpeGuestId
     * @param callback
     */
    public void modifyPrivilegedGuest(String shopSpeGuestId, String prlNum, MineBaseJsonRequestCallback<GuestsDetailData> callback) {
        Map<String, String> param = new HashMap<>();

        param.put("shopSpeGuestId", shopSpeGuestId);
        param.put("prlNum", prlNum);
        setUser(param);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_MODIFY_PRIVILEGED_GUEST))
                .params(param)
                .build().execute(callback);
    }

    /**
     * 添加特权用户
     *
     * @param requestUserId
     * @param userId
     * @param prlContent
     * @param imgUrls
     * @param prlNum
     * @param endTime
     * @param callback
     */
    public void addPrivilegedGuest(String requestUserId, String userId, String prlContent, String imgUrls, String prlNum, String endTime, MineBaseJsonRequestCallback<GuestsDetailData> callback) {
        Map<String, String> param = new HashMap<>();

        setUserShopId(param);

        param.put("requestUserId", requestUserId);
        param.put("userId", userId); //特权用户ID
        param.put("prlContent", prlContent); //	特权内容
        param.put("imgUrls", imgUrls); //嘉宾图片
        param.put("prlNum", prlNum); //特权次数
        param.put("endTime", endTime); // 特权到期时间

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_POST_ADD_PRIVILEGED_GUEST))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 通过手机号查找单个用户
     *
     * @param callback
     */
    public void searchUser(String mobile, MineBaseJsonRequestCallback<VipGustsData> callback) {
        Map<String, String> param = new HashMap<>();

        param.put("mobile", mobile);

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_SEARCH_USER_LIST))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 获取商家当前类型等信息
     *
     * @param callback
     */
    public void getObtainShopType(MineBaseJsonRequestCallback<ObtainShopType> callback) {

        Map<String, String> param = new HashMap<>();
        setUser(param);


        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_OBTAIN_SHOP_TYPE))
                .params(param)
                .build().execute(callback);
    }


    /**
     * 加入商家联盟/一卡通
     *
     * @param cardType
     * @param callback
     */
    public void JoinShopCardType(String cardType, String discount, MineBaseJsonRequestCallback<SharePondData> callback) {

        Map<String, String> param = new HashMap<>();
        setUser(param);
        param.put("cardType", cardType);
        if (StringUtils.isNotEmpty(discount)) {
            param.put("discount", discount);
        }

        OkHttpUtils.getInstance().post()
                .url(getUrl(APIUrls.OPERATION.TYPE_OPERATION_JOIN_SHOP_CARD_TYPE))
                .params(param)
                .build().execute(callback);
    }


}


